/*
 * @Author: czy
 * @Date: 2021-05-30 21:29:03
 * @LastEditTime: 2021-07-15 08:07:34
 */
#include "string.h"

#include "sys/system.h"
#include "sys/config.h"
#include "sys/type.h"

#include "../lib/partition.h"

extern unsigned int KMEMHandler;
extern unsigned int UMEMHandler;
extern unsigned int heapSize;

void *kmalloc(unsigned int size)
{
    unsigned int address = dPartitionAlloc(KMEMHandler, size);
    if (address == 0)
    {
        printk("Out of memory!!!");
    }
    return (void *)address;
}

void kfree(void *ptr)
{
    dPartitionFree(KMEMHandler, (unsigned int)ptr);
}

void allocblock(unsigned int *heap, unsigned int *stack)
{
    unsigned int handler = eFPartitionAlloc(UMEMHandler);
    handler = dPartitionInit(handler, heapSize);
    *heap = handler;
    *stack = handler + heapSize;
}

void freeblock(unsigned int heap)
{
    eFPartitionFree(UMEMHandler, heap);
}

void *brk(void)
{
    PCB *current = get_current_process();
    return (void *)(current->heap);
}
